package com.hoshiicloud.stock.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hoshiicloud.common.aop.Logable;
import com.hoshiicloud.common.exception.CommonError;
import com.hoshiicloud.common.exception.ServiceException;
import com.hoshiicloud.stock.entity.WarehouseList;
import com.hoshiicloud.stock.mapper.IWarehouseListMapper;
import com.hoshiicloud.stock.param.WarehouseListPageParam;
import com.hoshiicloud.stock.param.WarehousePageParam;
import com.hoshiicloud.stock.service.IWarehouseListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 库存Service接口实现类
 *
 * @author : yangcheng
 * @date : 2019/5/18 14:46
 * @mail : maiboyc@163.com
 **/
@Service
public class WarehouseListServiceImpl extends ServiceImpl<IWarehouseListMapper, WarehouseList> implements IWarehouseListService {

    /**
     * 根据id获取
     * @param id ID
     * @return Warehouse
     */
    @Cacheable(cacheNames = "warehouseList", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#id")
    @Logable(authName = "仓库管理-库存管理-根据ID查询库存管理信息")
    @Override
    public WarehouseList getById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据一组id获取
     * @param ids id组
     * @return List<WarehouseList>
     */
    @Cacheable(cacheNames = "warehouseList", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#ids")
    @Logable(authName = "仓库管理-库存管理-根据ID组查询库存管理信息")
    @Override
    public List<WarehouseList> getByIds(List<Long> ids) {
        return baseMapper.selectBatchIds(ids);
    }

    /**
     * 保存
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouseList", allEntries = true)
    @Logable(authName = "仓库管理-库存管理-新增库存管理信息")
    @Override
    public Long insert(WarehouseList entity) {
        //todo
        /**
         * 1、若为租户仓库 则只需检验对应 对应租户下 基础sku是否存在
         * 2、若为租户供应商仓库 对应租户供应商下 供应商sku是否存在
         * 3、若为平台仓库  对应平台下 基础sku是否存在
         * 4、若为平台供应商仓库 对应平台供应商下 供应商sku是否存在
         * 5、若为店铺仓库  对应平台下 基础sku是否存在
         * 6、若为店铺供应商仓库 店铺供应商下 供应商sku是否存在
         */
        // 创建日期 修改日期 创建人 修改人 删除标识
        entity.setCreatedTime(LocalDateTime.now());
        entity.setCreatedBy("admin");// 需要拿当前的登录人
        entity.setDeleted(0);
        int row = baseMapper.insert(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("新增库存管理信息失败"));
        }
        return entity.getId();
    }

    /**
     * 修改
     * @param entity 实体对象
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouseList", allEntries = true)
    @Logable(authName = "仓库管理-库存管理-修改库存管理信息")
    @Override
    public Long update(WarehouseList entity) throws ServiceException {
        // 自动填入修改时间 修改人
        entity.setUpdatedTime(LocalDateTime.now());
        entity.setUpdatedBy("admin1");// 需要拿当前的登录人
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("修改库存管理信息失败"));
        }
        return entity.getId();
    }

    /**
     * 查询
     * @param entity 查询参数实体
     * @return IPage<WarehouseList>
     */
    @Cacheable(cacheNames = "warehouseList", key = "#root.targetClass.simpleName+':'+#root.methodName+':'+#entity.pageIndex+':'+#entity.pageSize+':'+#entity.itemnoname+':'+#entity.skuno+':'+#entity.skuname")
    @Logable(authName = "仓库管理-库存管理-分页查询库存管理信息")
    @Override
    public IPage<WarehouseList> List(WarehouseListPageParam entity) {
        QueryWrapper<WarehouseList> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().like(StringUtils.isNotBlank(entity.getItemNoName()),WarehouseList::getItemnoname,entity.getItemNoName());
        queryWrapper.lambda().like(StringUtils.isNotBlank(entity.getSkuName()), WarehouseList::getSkuno,entity.getSkuNo());
        queryWrapper.lambda().like(StringUtils.isNotBlank(entity.getSkuName()),WarehouseList::getSkuname,entity.getSkuName());
        // 默认查询只查询未逻辑删除的数据
        queryWrapper.lambda().eq(entity.getDeleted()!=null,WarehouseList::getDeleted,entity.getDeleted());
        IPage<WarehouseList> page=new Page<>(entity.getPageIndex(),entity.getPageSize());
        return baseMapper.selectPage(page,queryWrapper);
    }

    /**
     * 删除
     * @param id ID
     * @return boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(cacheNames = "warehouseList", allEntries = true)
    @Logable(authName = "仓库管理-库存管理-删除库存管理信息")
    @Override
    public boolean delete(Long id) throws ServiceException {
        WarehouseList entity = this.getById(id);
        entity.setDeleted(1);
        int row = baseMapper.updateById(entity);
        if (row <= 0) {
            throw new ServiceException(CommonError.error("删除库存管理信息失败"));
        } else {
            return true;
        }
    }

}  